package com.rytong.tools.ui.jsfunction;
//#define Android2.2
import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.SoundPool;
//#ifdef Android2.2
import android.media.SoundPool.OnLoadCompleteListener;
import android.text.TextUtils;
//#endif
import com.rytong.tools.ui.LPWebView;
import com.rytong.tools.ui.LPWebView.MyLPWebView;
import com.rytong.tools.utils.Utils;

public class EmpAudio {
    int soundID;
    String pathString;
    SoundPool mSoundPool_;
    final String UNKNOWN_ERROR = "1";
    final String NO_FILE_ERROR = "2";
    final String PLAY_FAIL_ERROR = "3";
    final String PAUSE_FAIL_ERROR = "4";
    final String RESUME_FAIL_ERROR = "5";
    final String STOP_FAIL_ERROR = "6";
    final String GET_VOLUME_FAIL_ERROR = "7";
    final String SET_VOLUME_FAIL_ERROR = "8";

    public void load(MyLPWebView ewv , String soundname , String callback) {
    	int mMaxStreams = 1;
        mSoundPool_ = new SoundPool(mMaxStreams, AudioManager.STREAM_MUSIC, 0);
        try {
        	if(!TextUtils.isEmpty(soundname)){
        		if(soundname.startsWith("/")){// 网络音频
        		    soundname = soundname.substring(soundname.lastIndexOf("/") + 1, soundname.length());
        		    String sourceUrl = Utils.getConfigStringFormAsset((Activity)ewv.getContext(), "SERVER_URI").concat(soundname);// 下载地址
        		    String path = Utils.downloadAndSaveFile(sourceUrl , soundname);
        		    if(!TextUtils.isEmpty(path)){//文件下载成功
        		        //load方法 不成功返回0
        		        soundID = mSoundPool_.load(path, 1);
        		    }
        		} else {//本地音频
        		    Object  object= Utils.getLocalFilePath(soundname);
        		    if(object != null){
        		        if(object instanceof String){
        		            String path = (String)object;
        		            soundID = mSoundPool_.load(path , 1);
        		        } else if(object instanceof AssetFileDescriptor){
        		            soundID = mSoundPool_.load((AssetFileDescriptor)object, 1);
        		        }
        		    }
        		}
        	}
        		
        	if(!TextUtils.isEmpty(callback)){
        		ewv.loadUrl("javascript:".concat("var loadcallback = ").concat(callback).concat("; loadcallback();"));
        	}
        } catch (Exception e) {
            Utils.printException(e);
        }
    }

    public void dispose() {
    	if(mSoundPool_ != null){
    		mSoundPool_.release();
    	}
    	mSoundPool_ = null;
    }

    public void play(Object numberOfLoops) {
     // #ifdef Android2.2
    	if(mSoundPool_ != null){
    		    int loopTmp = (Integer) numberOfLoops;
    		    if(loopTmp > 0){
    		        loopTmp --;
    		    }
    		    final int loop = loopTmp;
    		    //loop:  0 = no loop, -1 = loop forever , other values indicate the number of repeats
    		    int result = mSoundPool_.play(soundID, 1.0f, 1.0f, 1, loop, 1.0f);
    		    if (result == 0) {// play 失败调用以下方法
    		        mSoundPool_.setOnLoadCompleteListener(new OnLoadCompleteListener() {
    		            @Override
    		            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
    		                // TODO Auto-generated method stub
    		                if (status == 0) {
    		                    mSoundPool_.play(soundID, 1.0f, 1.0f, 1, loop, 1.0f);
    		                }
                    }
    		    });
            }
        }
     // #endif
    }

    public void stop() {
    	if(mSoundPool_ != null){
    		mSoundPool_.stop(soundID);
    	}
    }

    public void pause() {
    	if(mSoundPool_ != null){
    		mSoundPool_.pause(soundID);
    	}
    }

    public void resume() {
    	if(mSoundPool_ != null){
    		mSoundPool_.resume(soundID);
    	}
    }

    public void getMaxVolume(MyLPWebView ewv, String callback) {
        AudioManager am = (AudioManager) Utils.getActivity().getSystemService(Utils.getActivity().AUDIO_SERVICE);
        int audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        ewv.loadUrl("javascript:".concat("var maxcallback = ").concat(callback).concat(";  maxcallback(0,").concat(String.valueOf(audioMaxVolumn)).concat(");"));
    }

    public void getMinVolume(MyLPWebView ewv, String callback) {
    	ewv.loadUrl("javascript:".concat("var mincallback = ").concat(callback).concat(";  mincallback(0,").concat(String.valueOf(0)).concat(");"));
    }

    public void getVolume(MyLPWebView ewv,String callback) {
        AudioManager am = (AudioManager) Utils.getActivity().getSystemService(Utils.getActivity().AUDIO_SERVICE);
        int audioCurrentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC);
        ewv.loadUrl("javascript:".concat("var volumecallback = ").concat(callback).concat(";  volumecallback(0,").concat(String.valueOf(audioCurrentVolumn)).concat(");"));
    }

    public void setVolume(MyLPWebView ewv, float volume, String callback) {
    	AudioManager am = (AudioManager) Utils.getActivity().getSystemService(Utils.getActivity().AUDIO_SERVICE);
    	// 最大音量序号。
    	int maxVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    	// 计算步长。各种机型的步长可能不一样。以100除以最大音量序号。
    	float stepVolume = 100.0f / maxVolume; // 得出每一阶段的音量index代表多少步长。
    	// 计算应该设置成多少序号。
    	int desVolumeIndex = (int) (volume / stepVolume);
    	desVolumeIndex = desVolumeIndex > maxVolume ? maxVolume : desVolumeIndex; // 最大值限制。
    	if(desVolumeIndex == 0 && volume != 0){ // 最小值修正。
    	    desVolumeIndex++;
    	} else if(desVolumeIndex == maxVolume && volume != 100){ // 最大值修正。
    	    desVolumeIndex--;
    	}
    	am.setStreamVolume(AudioManager.STREAM_MUSIC, desVolumeIndex, 0); 
    	ewv.loadUrl("javascript:".concat("var setcallback = ").concat(callback).concat(";  setcallback(0);"));
    }
}
